package com.demo.sbaclient.service.impl;

import com.demo.sbaclient.service.TestService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author Bruce
 * @date 2025/7/27 20:41
 */
@Service
public class TestServiceImpl implements TestService {
    @Override
    @Cacheable("testCache")
    public List<String> cache() {
        return getCache();
    }

    @Override
    @Cacheable(value = "USERS", key = "#id", sync = true)
    public String get(Long id) {
        return queryFromDB(id);
    }

    @Override
    @CacheEvict(value = "USERS", key = "#id")
    public void update(Long id) {
        System.out.println("updateUser: " + id);
    }

    private String queryFromDB(Long id) {
        System.out.println("queryFromDB: " + id);
        return "id: " + id;
    }

    private List<String> getCache() {
        System.out.println("cache");
        return Arrays.asList("1", "2");
    }
}
